package com.example.singleton;

import lombok.extern.slf4j.Slf4j;

import java.util.HashMap;

/**
 * @Description : 单例模式: 嵌套
 * @Author : Bruce Lee
 * @CreateTime : 2025/3/13
 */


@Slf4j
public class NestSingleton {

    private HashMap<String, Integer> map = new HashMap<>();

    private NestSingleton(){}

    private static class InnerNest{
        private static NestSingleton holder = new NestSingleton();
    }

    public static NestSingleton getNestSingleton(){
        return InnerNest.holder;
    }

    public void add(String key, Integer value){
        if (map.containsKey(key)){
            map.put(key, map.get(key) + value);
        } else{
            map.put(key, value);
        }
        log.info("add key:{} value:{}", key, value);
    }

    public void remove(String key){
        if (map.containsKey(key)){
            map.remove(key);
            log.info("remove key:{}", key);
        } else {
            log.info("key:{} not exist", key);
        }
    }

    public HashMap<String, Integer> printMap(){
        for (String key : map.keySet()) {
            log.info("key: {}, value: {}", key, map.get(key));
        }
        return map;
    }

}
